package trong.gold.soft.com.model;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

public class drawBackgr {
	 private LayerManager lm;
	 private Sprite actor;
	 private int gardenCols, gardenRows;
	 private int[][] bgIndexes;
	 private int [][] mang;
	 private int tam=0;
	public void drawbackgr(int width,int height,int bandau, Image backgr, Graphics g,int x,int y){
		gardenCols = width/x;
       gardenRows = width/x;
       bgIndexes = new int[gardenCols][gardenRows];
       for(int c=0; c<gardenCols; c++){
           for(int r=0; r<gardenRows; r++)
           	bgIndexes[c][r]=bandau;
       }
	     mang=new int[][]{
	             { 1, 2, 3, 4, 5, 6},
	             { 7, 8, 9, 10, 11, 12},
	             { 13, 14, 15, 16, 17, 18},
	             { 19, 20, 21, 22, 23, 24},
	             { 25 , 26, 27, 28, 29, 30},
	             { 31, 32, 33, 34, 35, 36},
	        };
	     if(width >=240 && height >=320){
	    	 for(int c=gardenCols/2 - mang[0].length/2; c<gardenCols/2 + mang[0].length/2; c++){
	             for(int r=0; r<mang.length; r++)
	            	 bgIndexes[c][r]=mang[tam][r];
	             tam+=1;
	         }

	     }else{
	    	 if(gardenCols >= mang[0].length){
	    		 for(int c=0; c<mang.length; c++){
		                for(int r=0; r<mang[c].length; r++)
		                	bgIndexes[c][r]=mang[c][r];
		            }
	    	 }
	    	 if(gardenCols < mang[0].length){
	    		 for(int c=0; c<gardenRows; c++){
		                for(int r=0; r<gardenCols; r++)
		                	bgIndexes[c][r]=mang[c][r];
		            }
	    	 }
	    	
	     }
	     lm = new LayerManager();
			TiledLayer background =new TiledLayer(gardenCols, gardenRows,backgr,x, y);
			 for(int c=0; c<gardenCols; c++){
	                for(int r=0; r<gardenRows; r++)
	                    background.setCell(c, r, bgIndexes[r][c]);
	            }
			    lm.append(background);
			    lm.paint(g, width/2 - backgr.getWidth()/2,0);
	}
	public void drawKhungG(Graphics g,Image img){
		 actor=new Sprite(img,180, 252);
	     actor.setFrame(0);
	     actor.setPosition(12, 12);
	     lm.append(actor);
	     lm.paint(g, 0, 0);
	}
}
